package cn.charlotte.pit.perk.type.boosts;

import cn.charlotte.pit.data.PlayerProfile;
import cn.charlotte.pit.parm.AutoRegister;
import cn.charlotte.pit.perk.*;
import cn.charlotte.pit.util.PlayerUtil;
import com.google.common.util.concurrent.AtomicDouble;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@AutoRegister
public class KungFuKnowledgePerk extends TypedPerk<PerksBoostsEnum> implements Listener {


    /* ───────── 基础信息 ───────── */
    @Override
    public String getInternalPerkName() {
        return getType().name();
    }

    @Override
    public String getDisplayName() {
        return "功夫知识";
    }

    @Override
    public Material getIcon() {
        return Material.BEEF;        // 若为 1.13+，请改用 Material.BEEF
    }

    @Override
    public double requireCoins() {
        return 10000.0;
    }

    @Override
    public double requireRenown(int level) {
        return 40.0;
    }

    @Override
    public int requirePrestige() {
        return 9;
    }

    @Override
    public int requireLevel() {
        return 100;
    }

    @Override
    public PerkType getPerkType() {
        return PerkType.PERK;
    }

    @Override
    public PerkGroup getPerkGroup() {
        return PerkGroup.BOOST;
    }

    @Override
    public List<String> getDescription(Player player) {
        return Arrays.asList(
                "&7武器不再造成伤害",
                "&7拳头将会附带高额伤害",
                "&7每4次攻击将会给予你 &b速度 II&7(0:05)"
        );
    }

    @Override
    public int getMaxLevel() {
        return 1;
    }

    /* ───────── 激活 / 失活 ───────── */
    @Override
    public void onPerkActive(Player player) {
        // 无额外逻辑
    }

    @Override
    public void onPerkInactive(Player player) {
        // 无额外逻辑
    }

    /* ───────── 事件监听 ───────── */
    @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
    public void onDamage(EntityDamageByEntityEvent event) {
        // 只关心玩家造成的伤害
        if (!(event.getDamager() instanceof Player)) return;

        Player damager = (Player) event.getDamager();
        if (!PlayerUtil.hasEquipPerk(damager, this)) return;

        // 判断主手是否空手（旧版可用 getItemInHand，1.9+ 建议使用 getInventory().getItemInMainHand）
        ItemStack hand = damager.getInventory().getItemInMainHand();
        ItemStack offHand = damager.getInventory().getItemInOffHand();
        if (hand.getType() != Material.AIR) {
            // 手持物品时取消全部伤害
            event.setDamage(0.0);
            return;
        }

        // 空手伤害：暴击 8.0，普通 7.3
        double newDamage = PlayerUtil.isCritical(damager) ? 8.0 : 7.3;
        event.setDamage(newDamage);
    }

    /* ───────── 自定义近战连击回调 ───────── */
    @Override
    public void handleAttackEntity(int enchantLevel, Player attacker, Entity target, double damage, AtomicDouble finalDamage, AtomicDouble boostDamage, AtomicBoolean cancel) {

        PlayerProfile profile = PlayerProfile.getPlayerProfileByUuid(attacker.getUniqueId());
        ItemStack hand = attacker.getInventory().getItemInMainHand();
        if (hand.getType() != Material.AIR) {
            return;
        }

        // 每第 4 次近战命中给予速度 II（5 秒）
        if (profile.getMeleeHit() % 4 == 0) {
            attacker.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20 * 5, 1), false);
        }
    }
}
